package top.timous.controller.teacher;

import cn.hutool.core.util.RandomUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.timous.pojo.CourseEntity;
import top.timous.pojo.ScoreEntity;
import top.timous.pojo.StudentEntity;
import top.timous.pojo.TeacherEntity;
import top.timous.service.TeacherService;
import top.timous.util.MD5Util;
import top.timous.util.RandImageUtil;
import top.timous.util.RedisUtil;
import top.timous.vo.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author luming
 * @Date 2020/12/4 11:02
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    private static final String BASE_CHECK_CODES = "qwertyuiplkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM1234567890";

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TeacherService teacherService;
    //登录
    @ApiOperation("教师登录")
    @PostMapping("/login")
    public AjaxResult<String> teacherLogin(@RequestBody TeacherLogin teacherLogin,HttpSession session) {
        System.out.println(teacherLogin);
        String password=MD5Util.MD5Encode(teacherLogin.getPassword(),"utf-8");
        System.out.println(password);
        String msg="";
        AjaxResult<String> ajaxResult=new AjaxResult<>();
        TeacherEntity teacherEntity = new TeacherEntity();
        teacherEntity.setTeaId(teacherLogin.getUserName());
        String key=teacherLogin.getCaptcha().toLowerCase();
        teacherEntity.setTeaPassword(password);
        System.out.println(teacherLogin);
        TeacherEntity teacher = teacherService.teacherLogin(teacherEntity);
        if (redisUtil.get("captcha")==null) {
            ajaxResult.setCode(1);
            msg="验证码已过期";
        }else {
            if (key.equals(redisUtil.get("captcha"))) {
                if (teacher==null) {
                    msg="用户名或密码错误";
                    ajaxResult.setCode(1);
                }else {
                    teacher.setTeaPassword("");
                    session.setAttribute("teacher", teacher);
                    msg = "登录成功";
                    ajaxResult.setCode(0);
                }
            }else {
                ajaxResult.setCode(1);
                msg="验证码错误";
            }
        }
        String username="";
        if (teacher!=null) {
            username=teacher.getTeaName();
        }


        ajaxResult.setData(username);
        System.out.println(msg);
        System.out.println(teacher);
        ajaxResult.setMsg(msg);


        //ajaxResult.setData(teacher);

        return ajaxResult;
    }
    @ApiOperation("获取验证码的值")
    @GetMapping("/getCode")
    public AjaxResult<String> getCode(){

        String realKey = (String) redisUtil.get("captcha");
        AjaxResult<String> res = new AjaxResult<String>();
        res.setData(realKey);
        res.setMsg("请求成功！");
        res.setCode(1);
        return res;
    }

    /**
     * 验证码
     * 别人做的，返回的结果集需要修改
     * @param key
     * @return
     */
    @ApiOperation("获取验证码")
    @GetMapping(value = "/randomImage")
    public AjaxResult<String> randomImage(String key){
        AjaxResult<String> res = new AjaxResult<String>();
        try {
            String code = RandomUtil.randomString(BASE_CHECK_CODES,4);
            String lowerCaseCode = code.toLowerCase();
            String realKey = "captcha";
            //每次刷新验证码的时候  把验证码缓存一分钟
            redisUtil.set(realKey, lowerCaseCode, 120);
            String base64 = RandImageUtil.generate(code);
            res.setData(base64);
            System.out.println(code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @ApiOperation("查询所有学生信息")
    @GetMapping("/queryAllStudentInfo")
    AjaxResult<List<StudentInfos>> queryAllStudentInfo(StudentInfos studentInfos) {
        int page = (studentInfos.getPage()-1) * studentInfos.getLimit();
        studentInfos.setPage(page);
        String msg="";
        AjaxResult<List<StudentInfos>> ajaxResult=new AjaxResult<>();
        List<StudentInfos> students = new ArrayList<>();
        List<StudentEntity> list = teacherService.queryAllStudentInfo(studentInfos);
        if (list!=null) {
            for (StudentEntity studentEntity : list) {
                StudentInfos studentInfos1 = new StudentInfos();
                studentInfos1.setStuId(studentEntity.getStuId());
                studentInfos1.setStuName(studentEntity.getStuName());
                studentInfos1.setClassName(teacherService.queryClassNameById(studentEntity.getClassId()));
                studentInfos1.setHomeName(teacherService.queryHomeNameById(studentEntity.getHomeId()));
                if (studentEntity.getSex() == 0) {
                    //该学生是男的
                    studentInfos1.setSex("男");
                } else {
                    //该学生是女生
                    studentInfos1.setSex("女");
                }
                students.add(studentInfos1);
                msg="查询成功";
            }
        }else {
            msg="没有该数据";
        }
        int count=teacherService.queryAllStudentInfoCount(studentInfos);
        ajaxResult.setMsg(msg);
        ajaxResult.setData(students);
        ajaxResult.setCount(count);
        ajaxResult.setCode(0);
        return ajaxResult;
    }

//    @ApiOperation("查询所有学生信息")
//    @GetMapping("/queryAllStudentInfo")
//    AjaxResult<List<StudentInfos>> queryAllStudentInfo(StudentInfos studentInfos) {
//        String msg="";
//        AjaxResult<List<StudentInfos>> ajaxResult=new AjaxResult<>();
//        List<StudentInfos> students = new ArrayList<>();
//        List<StudentEntity> list = teacherService.queryAllStudentInfo();
//        for (StudentEntity studentEntity : list) {
//            StudentInfos studentInfos1 = new StudentInfos();
//            studentInfos1.setStuId(studentEntity.getStuId());
//            studentInfos1.setStuName(studentEntity.getStuName());
//            studentInfos1.setClassName(teacherService.queryClassNameById(studentEntity.getClassId()));
//            studentInfos1.setHomeName(teacherService.queryHomeNameById(studentEntity.getHomeId()));
//            if(studentEntity.getSex() == 0){
//                //该学生是男的
//                studentInfos1.setSex("男");
//            }else{
//                //该学生是女生
//                studentInfos1.setSex("女");
//            }
//            students.add(studentInfos1);
//        }
//        msg="查询成功";
//        ajaxResult.setCode(0);
//        int count=teacherService.queryAllStudentInfoCount();
//        ajaxResult.setMsg(msg);
//        ajaxResult.setData(students);
//        ajaxResult.setCount(count);
//        return ajaxResult;
//    }

    @ApiOperation("添加学生信息")
    @PostMapping("/addStudentInfo")
    public AjaxResult<String> addStudentInfo(@RequestBody StudentInfos studentInfos){
        String msg="";
        AjaxResult<String> ajaxResult=new AjaxResult<>();
        StudentEntity studentEntity = new StudentEntity();
        studentEntity.setStuId(studentInfos.getStuId());
        studentEntity.setStuName(studentInfos.getStuName());
        studentEntity.setStuPassword(MD5Util.MD5Encode("12345","utf-8"));
        int sex=studentInfos.getSex().equals("男")?0:1;
        studentEntity.setSex(sex);
        studentEntity.setClassId(teacherService.queryclassIdByclassName(studentInfos.getClassName()));
        studentEntity.setHomeId(teacherService.queryhomeIdByhomeName(studentInfos.getHomeName()));
        System.out.println(studentInfos);
        int r = teacherService.addStudentInfo(studentEntity);
        ajaxResult.setCode(0);
        ajaxResult.setData("");
        if (r==1) {
            msg="添加失败,该学号已存在";
        }else {
            msg="添加成功";
        }
        ajaxResult.setMsg(msg);
        return ajaxResult;
    }
    @ApiOperation("根据id查学生信息")
    @PostMapping("/queryStuInfoByStuId")
    AjaxResult<StudentInfos> queryStuInfoByStuId(String stuId) {
        System.out.println("=======================================");
        System.out.println(stuId);
        String msg="";
        AjaxResult<StudentInfos> ajaxResult=new AjaxResult<>();
        StudentInfos studentInfos=new StudentInfos();
        StudentEntity studentEntity=teacherService.queryStuInfoByStuId(stuId);
        System.out.println(studentEntity);
        studentInfos.setStuId(studentEntity.getStuId());
        studentInfos.setStuName(studentEntity.getStuName());
        studentInfos.setSex(studentEntity.getSex()==0?"男":"女");
        studentInfos.setClassName(teacherService.queryClassNameById(studentEntity.getClassId()));
        studentInfos.setHomeName(teacherService.queryHomeNameById(studentEntity.getHomeId()));
        ajaxResult.setData(studentInfos);
        msg="查询成功";
        ajaxResult.setMsg(msg);
        ajaxResult.setCode(0);
        return ajaxResult;
    }

    @ApiOperation("批量删除学生信息")
    @PostMapping("/deleteStudentInfos")
    public AjaxResult<String> deleteStudentInfos(@RequestBody List<StudentInfos> studentInfos) {
        AjaxResult<String> ajaxResult=new AjaxResult<>();
        String[] ids = new String[studentInfos.size()];
        int i=0;
        for (StudentInfos student:studentInfos) {
            ids[i]=student.getStuId();
            i++;
        }
        for (int j = 0; j < ids.length; j++) {
            System.out.println(ids[j]);
        }
        teacherService.deleteStudentInfos(ids);
        ajaxResult.setCode(0);
        ajaxResult.setMsg("删除成功");
        return ajaxResult;

    }

    @ApiOperation("删除一条学生信息")
    @PostMapping("/deleteStudentInfoById")
    AjaxResult<String> deleteStudentInfo(String id) {
        String msg="";
        AjaxResult<String> ajaxResult = new AjaxResult<>();
        teacherService.deleteStudentInfo(id);
        ajaxResult.setMsg(msg);
        ajaxResult.setData("");
        return ajaxResult;


    }

    @ApiOperation("修改教师密码")
    @PostMapping("/updatePassword")
    AjaxResult<String> updatePassword(@RequestBody TeacherPassword teacherPassword,HttpSession session) {
        System.out.println(teacherPassword);
        String msg="";
        TeacherEntity teacher = (TeacherEntity) session.getAttribute("teacher");
        System.out.println(teacher);
        AjaxResult<String> res = new AjaxResult<>();
        teacher.setTeaPassword(MD5Util.MD5Encode(teacherPassword.getOldPassword(),"utf-8"));
        //System.out.println(originalPwd);
        TeacherEntity teacherEntity = teacherService.queryOwnInfo(teacher);
        System.out.println(teacherEntity);
        String newPwd=teacherPassword.getNewPassword();
        if (teacherEntity != null){
            teacher.setTeaPassword(MD5Util.MD5Encode(newPwd,"utf-8"));
            teacherService.updatePassword(teacher);
            res.setCode(0);
            msg="修改成功";

        }else{
            res.setCode(1);
            msg="修改失败";
        }
        res.setData(newPwd);
        res.setMsg(msg);
        return res;

    }


    @ApiOperation("查询自己信息")
    @GetMapping("/queryOwnInfo")
    AjaxResult<TeacherEntity> queryOwnInfo(HttpSession session) {
        String msg="";
        AjaxResult<TeacherEntity> ajaxResult=new AjaxResult<>();
        TeacherEntity teacher = (TeacherEntity) session.getAttribute("teacher");
        System.out.println(teacher);
        teacher.setTeaName(teacher.getTeaName());
        TeacherEntity ownInfo = teacherService.queryOwnInfo(teacher);
        System.out.println(ownInfo);
        if (ownInfo==null) {
            ajaxResult.setCode(1);
            msg="查询失败";
        }else {
            ajaxResult.setCode(0);
            msg="查询成功";
        }
        ajaxResult.setMsg(msg);
        ajaxResult.setData(ownInfo);
        return ajaxResult;
    }

    @ApiOperation("根据学号查询学生成绩")
    @GetMapping("/queryStuScoreByStuId")
    AjaxResult<List<StudentbyPassword>> queryStuScoreByStuId(String stuId) {
        String msg="";
        AjaxResult<List<StudentbyPassword>> ajaxResult= new AjaxResult<>();
        StudentbyPassword studentEntity=new StudentbyPassword();
        studentEntity.setStuId(stuId);
        List<StudentbyPassword> list= teacherService.queryStuScoreByStuId(studentEntity);
        System.out.println(list);
        ajaxResult.setData(list);
        if (list.size()!=0) {
            ajaxResult.setCode(0);
            msg="查询成功";

        }else {
            ajaxResult.setCode(1);
            msg="没有该同学的学号";
        }
        ajaxResult.setMsg(msg);
        return ajaxResult;
    }

    @ApiOperation("查询自己授课信息")
    @GetMapping("/queryclassInfo")
    AjaxResult<List<TeacherCourse>> queryclassInfo(HttpSession session) {
        String msg="";
        TeacherEntity teacher = (TeacherEntity) session.getAttribute("teacher");
        System.out.println(teacher);
        AjaxResult<List<TeacherCourse>> ajaxResult=new AjaxResult<>();
        String teaId=teacher.getTeaId();
        List<TeacherCourse> teacherCourses=teacherService.queryclassInfo(teaId);
        System.out.println(teacherCourses);
        List<TeacherCourse> list=new ArrayList<>();
        for (TeacherCourse t:teacherCourses) {
            TeacherCourse teacherCourse=new TeacherCourse();
            teacherCourse.setCouId(t.getCouId());
            teacherCourse.setCouName(t.getCouName());
            teacherCourse.setCredit(t.getCredit());
            list.add(teacherCourse);
        }
        msg="查询成功";
        ajaxResult.setCode(0);
        ajaxResult.setData(list);
        ajaxResult.setMsg(msg);
        return ajaxResult;
    }
    //
//    @ApiOperation("查询自己教授的课程的学生成绩")
//    @GetMapping("/queryStudentScore")
//    AjaxResult<List<StudentScore>> queryStudentScore(HttpSession session) {
//        String msg="";
//        TeacherEntity teacher = (TeacherEntity) session.getAttribute("teacher");
//        String teaId=teacher.getTeaId();
//        AjaxResult<List<StudentScore>> ajaxResult=new AjaxResult<>();
//        List<StudentScore> studentScores=teacherService.queryStudentScore(teaId);
//        List<StudentScore> list=new ArrayList<>();
//        for (StudentScore s:studentScores) {
//            StudentScore studentScore=new StudentScore();
//            studentScore.setStuId(s.getStuId());
//            studentScore.setStuName(s.getStuName());
//            studentScore.setCouName(s.getCouName());
//            studentScore.setScore(s.getScore());
//            studentScore.setCredit(s.getCredit());
//            list.add(studentScore);
//        }
//        System.out.println(list);
//        msg="查询成功";
//        //int count=teacherService.queryStudentScoreCount();
//        ajaxResult.setMsg(msg);
//        ajaxResult.setCode(0);
//        ajaxResult.setData(list);
//        //ajaxResult.setCount(5);
//        return ajaxResult;
//    }

    @ApiOperation("查询自己教授的课程的学生成绩")
    @GetMapping("/queryStudentScore")
    AjaxResult<List<StudentScore>> queryStudentScore(HttpSession session,StudentScore studentScore) {
        System.out.println("=================================");
        System.out.println(studentScore);
        System.out.println("=================================");
        int page = (studentScore.getPage()-1) * studentScore.getLimit();
        studentScore.setPage(page);
        String msg="";
        TeacherEntity teacher = (TeacherEntity) session.getAttribute("teacher");
        String teaId=teacher.getTeaId();
        studentScore.setTeaId(teaId);
        System.out.println("=================================");
        System.out.println(teaId);
        System.out.println("=================================");
        AjaxResult<List<StudentScore>> ajaxResult=new AjaxResult<>();
        List<StudentScore> studentScores=teacherService.queryStudentScore(studentScore);
        List<StudentScore> list = new ArrayList<>();
        if (studentScores!=null) {
            for (StudentScore s : studentScores) {
                StudentScore studentScore1 = new StudentScore();
                studentScore1.setStuId(s.getStuId());
                studentScore1.setStuName(s.getStuName());
                studentScore1.setCouName(s.getCouName());
                studentScore1.setScore(s.getScore());
                studentScore1.setCredit(s.getCredit());
                list.add(studentScore1);
                msg="查询成功";
            }
        }else {
            msg="没有该数据";
        }
        System.out.println(list);

        int count=teacherService.queryStudentScoreCount(studentScore);
        ajaxResult.setMsg(msg);
        ajaxResult.setCode(0);
        ajaxResult.setData(list);
        ajaxResult.setCount(count);
        return ajaxResult;
    }
    //
//    @ApiOperation("根据id查学生成绩")
//    @PostMapping("/queryStuscoreByStuId")
//    AjaxResult<StudentScore> queryStuscoreByStuId(String stuId,String couName,HttpSession session) {
//        System.out.println("====================================================");
//        TeacherEntity teacher = (TeacherEntity) session.getAttribute("teacher");
//        System.out.println("====================================================");
//        String teaId=teacher.getTeaId();
//        System.out.println(stuId);
//        System.out.println("====================================================");
//        String msg="";
//        AjaxResult<StudentScore> ajaxResult=new AjaxResult<>();
//        StudentScore studentScore=new StudentScore();
//
//
//    }
    @ApiOperation("修改学生信息")
    @PostMapping("/updateStudentInfo")
    AjaxResult<StudentInfos> updateStudentInfo(@RequestBody StudentInfos studentInfos) {
        System.out.println("==================================================");
        System.out.println(studentInfos);
        System.out.println("==================================================");

        AjaxResult<StudentInfos> ajaxResult=new AjaxResult<>();
        StudentEntity studentEntity=new StudentEntity();
        studentEntity.setStuId(studentInfos.getStuId());
        studentEntity.setStuName(studentInfos.getStuName());
        studentEntity.setClassId(teacherService.queryclassIdByclassName(studentInfos.getClassName()));
        studentEntity.setHomeId(teacherService.queryhomeIdByhomeName(studentInfos.getHomeName()));
        studentEntity.setSex(studentInfos.getSex().equals("男")?0:1);
        teacherService.updateStudentInfoById(studentEntity);
        ajaxResult.setCode(0);
        ajaxResult.setMsg("修改成功");
        return ajaxResult;

    }
    @ApiOperation("修改学生成绩")
    @PostMapping("/updateScore")
    AjaxResult<StudentScore> updateScore(@RequestBody StudentScore studentScore) {
        System.out.println("==================================================");
        System.out.println(studentScore);
        System.out.println("==================================================");
        AjaxResult<StudentScore> ajaxResult=new AjaxResult<>();
        StudentScore student=new StudentScore();
        student.setStuId(studentScore.getStuId());
        student.setScore(studentScore.getScore());
        teacherService.updateScore(student);
        ajaxResult.setMsg("修改成功");
        ajaxResult.setCode(0);
        return ajaxResult;
    }
    @ApiOperation("录入学生成绩")
    @PostMapping("/addscore")
    AjaxResult<String> addscore(@RequestBody StudentScore studentScore) {
        String msg="";
        System.out.println("===================================================");
        System.out.println(studentScore);
        System.out.println("===================================================");
        AjaxResult<String> ajaxResult=new AjaxResult<>();
        ScoreEntity scoreEntity=new ScoreEntity();
        scoreEntity.setStuId(studentScore.getStuId());
        scoreEntity.setStuName(teacherService.queryStuNameByStuId(studentScore.getStuId()));
        scoreEntity.setCouId(teacherService.queryCouIdByCouName(studentScore.getCouName()));
        scoreEntity.setScore(studentScore.getScore());
        scoreEntity.setCredit(teacherService.queryCreditByCouName(studentScore.getCouName()));
        teacherService.addScore(scoreEntity);
        ajaxResult.setCode(0);
        ajaxResult.setMsg("添加成功");
        return ajaxResult;
//        StudentEntity studentEntity = new StudentEntity();
//        studentEntity.setStuId(studentInfos.getStuId());
//        studentEntity.setStuName(studentInfos.getStuName());
//        studentEntity.setStuPassword(MD5Util.MD5Encode("12345","utf-8"));
//        int sex=studentInfos.getSex().equals("男")?0:1;
//        studentEntity.setSex(sex);
//        studentEntity.setClassId(teacherService.queryclassIdByclassName(studentInfos.getClassName()));
//        studentEntity.setHomeId(teacherService.queryhomeIdByhomeName(studentInfos.getHomeName()));
//        System.out.println(studentInfos);
//        int r = teacherService.addStudentInfo(studentEntity);
//        ajaxResult.setCode(r);
//        ajaxResult.setData("");
//        if (r==1) {
//            msg="添加失败,该学号已存在";
//        }else {
//            msg="添加成功";
//        }


    }

    @ApiOperation("给数据表格赋值")
    @GetMapping("/getJson")
    public AjaxResult<List<Integer>> getJson(HttpSession session){
        List<Integer> list = new ArrayList<>();
        TeacherEntity teacherEntity = (TeacherEntity) session.getAttribute("teacher");
        Amount amount = new Amount();
        amount.setTeaId(teacherEntity.getTeaId());
        // 查询成绩
        int[] score = new int[6];
        //查询0-50分的数据
        amount.setStart(0);
        amount.setEnd(50);
        score[0] = teacherService.queryAmount(amount);
        //50-60分的数据
        amount.setStart(50);
        amount.setEnd(60);
        score[1] = teacherService.queryAmount(amount);
        //60-70的数据
        amount.setStart(60);
        amount.setEnd(70);
        score[2] = teacherService.queryAmount(amount);
        // 70-80的数据
        amount.setStart(70);
        amount.setEnd(80);
        score[3] = teacherService.queryAmount(amount);
        // 80-90的数据
        amount.setStart(80);
        amount.setEnd(90);
        score[4] = teacherService.queryAmount(amount);
        // 90 - 100的数据
        amount.setStart(90);
        amount.setEnd(100);
        score[5] = teacherService.queryAmount(amount);
        for (int i : score) {
            list.add(score[i]);
        }
        AjaxResult<List<Integer>> res = new AjaxResult<>();
        res.setCode(0);
        res.setMsg("查询成功");
        res.setData(list);
        return res;
    }

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        System.out.println(list);
    }


}
